package com.LeeCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 四数相加 II
 */

public class Code454 {
    public static void main(String[] args) {

    }

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> targets = new HashMap<>();
        int n = nums1.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                targets.merge(nums1[i] + nums2[j], 1, Integer::sum);
            }
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int target = -(nums3[i] + nums4[j]);
                ans += targets.getOrDefault(target, 0);
            }
        }

        return ans;
    }
}
